function taoOptionsChoddlQuanHuyen(tinhThanhDuocChon, khuvucDoc, ddlQuanHuyen) {
    var xPath = "//tinhthanh[tentinhthanh='" + tinhThanhDuocChon + "']/quanhuyen";
    //alert(xPath);
    //alert(xmlToString(khuvucDoc));
    var dsQuanHuyen = queryLists(xPath, khuvucDoc, null);

    while ( ddlQuanHuyen.options.length ) {
        ddlQuanHuyen.options[0] = null;
    }
    if ('ddlQuanHuyenSearch'==ddlQuanHuyen.id){
        var option = new Option("Tất cả quận huyện", "All");
        ddlQuanHuyen.options.add(option);
    }
    for (var i = 0; i< dsQuanHuyen.length; i++) {
        //var quanhuyenName = dsQuanHuyen.item(i).nodeName;
        var quanHuyenTemp = dsQuanHuyen[i].childNodes;
        var qHTemp = quanHuyenTemp.item(0).nodeValue;
        //alert(i + "name" + quanhuyenName + qHTemp);
        var optn = document.createElement("OPTION");
        optn.text = qHTemp;
        optn.value = qHTemp;
        ddlQuanHuyen.options.add(optn);
    }
}

function changeQuanHuyen(ddlTinhThanh, ddlQuanHuyen) {
    //alert(ddlTinhThanh);
    //alert(ddlQuanHuyen.id);
    if ('ddlQuanHuyenSearch'==ddlQuanHuyen.id){
        changeKeySearch();
    }
    var ddlTT = document.getElementById(ddlTinhThanh);
    var tinhthanhduocchon = ddlTT.options[ddlTT.selectedIndex].text;
    taoOptionsChoddlQuanHuyen(tinhthanhduocchon, khuvucDoc, ddlQuanHuyen);
}

function trimString(id){
    var s = document.getElementById(id).value;
    s = s.replace(/\s/g, '');
    document.getElementById(id).value = s;
}

function checkFloatNumber(str){
    //alert(str);
    var RE_FLOAT = /^[0-9]*\.?[0-9]+$/;
    return RE_FLOAT.test(str);
}

function changeKeySearch() {
    var bSear = document.getElementById('bSearching').value;
    if (bSear=='true'){
        //document.getElementById('bchangeKeySearch').value = 'true';
        document.getElementById('numOfPageSearch').value = 0;
    }
}

function validateKeySearch(){
    var errorMsg = "";
    var giatienFrom = document.getElementById('txtSearchGiaTienFrom').value;
    var giatienTo = document.getElementById('txtSearchGiaTienTo').value;
    if(giatienFrom != ""){
        if (!checkFloatNumber(giatienFrom)) {
            errorMsg += giatienFrom + " không phải là một số!" + "\n";
        }
    }
    if(giatienTo != ""){
        if (!checkFloatNumber(giatienTo)) {
            errorMsg += giatienTo + " không phải là một số!" + "\n";
        }
    }

    if ("" == errorMsg) {
        return true;
    }
    alert(errorMsg);
    return false;


}

function taoxpathmainsearch() {

    var xpath = "//tindang[@status!='delete'";

    var loaitinKey = "";
    var loainhaKey = "";
    var loaigiaytoKey = "";
    var tinhthanhKey = "";
    var quanhuyenKey = "";
    var giatienfromKey = "";
    var giatientoKey = "";
    var loaitienteKey = "";

    var loaitinKeyDDL = document.getElementById('ddlLoaiTin');
    //if ('All' != loaitinKeyDDL.options[loaitinKeyDDL.selectedIndex].value) {
    loaitinKey = loaitinKeyDDL.options[loaitinKeyDDL.selectedIndex].text;
    xpath = xpath + " and tenloaitin='" + loaitinKey +"'";
    //}

    var loainhaKeyDDL = document.getElementById('ddlLoaiNhaSearch');
    if ('All' != loainhaKeyDDL.options[loainhaKeyDDL.selectedIndex].value) {
        loainhaKey = loainhaKeyDDL.options[loainhaKeyDDL.selectedIndex].text;
        xpath = xpath + " and tenloainha='" + loainhaKey + "'";
    }

    var loaigiaytoKeyDDL = document.getElementById('ddlLoaiGiayToSearch');
    if ('All' != loaigiaytoKeyDDL.options[loaigiaytoKeyDDL.selectedIndex].value) {
        loaigiaytoKey = loaigiaytoKeyDDL.options[loaigiaytoKeyDDL.selectedIndex].text;
        xpath = xpath + " and tenloaigiayto='" + loaigiaytoKey + "'";
    }

    var tinhthanhKeyDDL = document.getElementById('ddlTinhThanhSearch');
    if ('All' != tinhthanhKeyDDL.options[tinhthanhKeyDDL.selectedIndex].value) {
        tinhthanhKey = tinhthanhKeyDDL.options[tinhthanhKeyDDL.selectedIndex].text;
        xpath = xpath + " and khuvuc/tinhthanh='" + tinhthanhKey + "'";
    }

    var quanhuyenKeyDDL = document.getElementById('ddlQuanHuyenSearch');
    if ('All' != quanhuyenKeyDDL.options[quanhuyenKeyDDL.selectedIndex].value) {
        quanhuyenKey = quanhuyenKeyDDL.options[quanhuyenKeyDDL.selectedIndex].text;
        xpath = xpath + " and khuvuc/quanhuyen='" + quanhuyenKey + "'";
    }

    giatienfromKey = document.getElementById('txtSearchGiaTienFrom').value;
    if ('' != giatienfromKey) {
        xpath = xpath + " and giatien/solieu>=" + giatienfromKey;
    }
    giatientoKey = document.getElementById('txtSearchGiaTienTo').value;
    if ('' != giatientoKey) {
        xpath = xpath + " and giatien/solieu<=" + giatientoKey;
    }

    var loaitienteKeyDDL = document.getElementById('ddlLoaiTienTeSearch');
    if ('All' != loaitienteKeyDDL.options[loaitienteKeyDDL.selectedIndex].value){
        if (('' != giatienfromKey) || ('' != giatientoKey)) {
            loaitienteKey = loaitienteKeyDDL.options[loaitienteKeyDDL.selectedIndex].text;
            xpath = xpath + " and giatien/dvdgiatien='" + loaitienteKey + "'";
        }
    }

    //alert( loaitinKey + loainhaKey + loaigiaytoKey + tinhthanhKey + quanhuyenKey + giatienfromKey + giatientoKey + loaitienteKey);

    xpath = xpath + "]";
    return xpath;
}

function clearTatCa(prefix){
    for(var i=1;i<=5;i++){
        var temp = document.getElementById(prefix+'imagetin'+i);
        if(temp != null){
            document.getElementById(prefix+'imagetin'+i).src='';
        }
        temp = document.getElementById(prefix+'tieudetin'+i);
        if(temp != null){
            document.getElementById(prefix+'tieudetin'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'lancuoicapnhat'+i);
        if(temp != null){
            document.getElementById(prefix+'lancuoicapnhat'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'quanhuyen'+i);
        if(temp != null){
            document.getElementById(prefix+'quanhuyen'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'tinhthanh'+i);
        if(temp != null){
            document.getElementById(prefix+'tinhthanh'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'dtkv'+i);
        if(temp != null){
            document.getElementById(prefix+'dtkv'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'dvddtkv'+i);
        if(temp != null){
            document.getElementById(prefix+'dvddtkv'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'dtxd'+i);
        if(temp != null){
            document.getElementById(prefix+'dtxd'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'dvddtxd'+i);
        if(temp != null){
            document.getElementById(prefix+'dvddtxd'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'giatien'+i);
        if(temp != null){
            document.getElementById(prefix+'giatien'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'dvdgiatien'+i);
        if(temp != null){
            document.getElementById(prefix+'dvdgiatien'+i).innerHTML = "";
        }
        temp = document.getElementById(prefix+'msts'+i);
        if(temp != null){
            document.getElementById(prefix+'msts'+i).value = "";
        }
        temp = document.getElementById(prefix+'table'+i);
        if(temp != null){
            document.getElementById(prefix+'table'+i).style.display = 'none';
        }
    }
    
}

function showresult (arr, start, end, page) {
    //alert(page + "====" +start + "-----" + end)
    document.getElementById('h3_tinmoinhat').innerHTML = 'Kết Quả Tìm Kiếm';
    //var page = Number(document.getElementById('numOfPageSearch').value);
    var loaitinKeyDDL = document.getElementById('ddlLoaiTin');
    var loaitin = loaitinKeyDDL.options[loaitinKeyDDL.selectedIndex].text;
    var prefix_loaitin = "";
    if ('Cần Bán'==loaitin) {
        prefix_loaitin = "ban_";
    } else if ('Cần Mua'==loaitin) {
        prefix_loaitin = "mua_";
    } else if ('Cần Thuê'==loaitin) {
        prefix_loaitin = "canthue_";
    } else if ('Cho Thuê'==loaitin) {
        prefix_loaitin = "chothue_";
    }
    clearTatCa('ban_');
    clearTatCa('mua_');
    clearTatCa('canthue_');
    clearTatCa('chothue_');
    //var start = page*5;
    //var end = page*5;
    for (var i=start;i<=end;i++){
        var nodeTemp = arr[i-1];
        for (var j=0;j<nodeTemp.childNodes.length;j++){
            //alert(node1.childNodes.item(i).nodeName);
            var tempstr;
            if (nodeTemp.childNodes.item(j).nodeName == 'matindang'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                    document.getElementById(prefix_loaitin+'msts'+(i - (page)*5)).value=tempstr;
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloainha'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloaigiayto'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            } else if (nodeTemp.childNodes.item(j).nodeName == 'dientichkhuonvien'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                        var dvdkhuonvienTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'dvddtkv'+(i - (page)*5)).innerHTML=dvdkhuonvienTemp;
                    }
                    if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                        var solieukhuonvienTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'dtkv'+(i - (page)*5)).innerHTML= solieukhuonvienTemp;
                    }
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'dientichxaydung'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                        var dvdxaydungTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'dvddtxd'+(i - (page)*5)).innerHTML=dvdxaydungTemp;
                    }
                    if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                        var solieuxaydungTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'dtxd'+(i - (page)*5)).innerHTML= solieuxaydungTemp;
                    }
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'khuvuc'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                        var quanhuyenTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'quanhuyen'+(i - (page)*5)).innerHTML=quanhuyenTemp;
                    }
                    if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                        var tinhthanhTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'tinhthanh'+(i - (page)*5)).innerHTML=tinhthanhTemp;
                    }
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'giatien'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                        var dvdgiatienTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'dvdgiatien'+(i - (page)*5)).innerHTML=dvdgiatienTemp;
                    }
                    if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                        var giatienTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                        document.getElementById(prefix_loaitin+'giatien'+(i - (page)*5)).innerHTML= giatienTemp;
                    }
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'hinhanh'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                    //alert (prefix_loaitin+'imagetin'+(i - (page)*5) + "---" + tempstr);
                    document.getElementById(prefix_loaitin+'imagetin'+(i - (page)*5)).src=tempstr;
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'tieudetin'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                    //alert (prefix_loaitin+'imagetin'+(i - (page)*5) + "---" + tempstr);
                    document.getElementById(prefix_loaitin+'tieudetin'+(i - (page)*5)).innerHTML=tempstr;
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloaitin'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            } else if (nodeTemp.childNodes.item(j).nodeName == 'nguoidangtin'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            } else if (nodeTemp.childNodes.item(j).nodeName == 'ngaydangtin'){
            //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
            } else if (nodeTemp.childNodes.item(j).nodeName == 'lancuoicapnhat'){
                if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                    tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                    document.getElementById(prefix_loaitin+'lancuoicapnhat'+(i - (page)*5)).innerHTML=tempstr;
                }
            } else if (nodeTemp.childNodes.item(j).nodeName == 'soluotxem'){
        //str1 = nodeTemp.childNodes.item(i).childNodes.item(0).nodeValue;
        }
        }
        //alert(prefix_loaitin+'table'+(i - (page)*5));
        document.getElementById(prefix_loaitin+'table'+(i - (page)*5)).style.display = 'block';
    }
}

function showketquatruoc() {
    var bChange = document.getElementById('bchangeKeySearch').value;
    if (bChange == 'false'){
        var page = Number(document.getElementById('numOfPageSearch').value);
        mainsearch(page-1);
    } else {
        document.getElementById('numOfPageSearch').value = 0;
        mainsearch(0);
    }
}

function showketquasau() {
    var bChange = document.getElementById('bchangeKeySearch').value;
    if (bChange == 'false'){
        var page = Number(document.getElementById('numOfPageSearch').value);
        mainsearch(page+1);
    } else {
        document.getElementById('numOfPageSearch').value = 0;
        mainsearch(0);
    }
}

function mainsearch(page) {
    var currentpage = Number(document.getElementById('numOfPageSearch').value);
    var xpathfull="";
    if (validateKeySearch()){
        xpathfull = taoxpathmainsearch();
        //alert(xpathfull);
        //alert(xmlToString(tindangDoc));
        var dsTinDangResult = queryLists(xpathfull, tindangDoc, null);

        var dsTinDangResultArr = [];
        dsTinDangResultArr = nodeListTinDangToArray(dsTinDangResult);
        if(dsTinDangResultArr.length > 0){
            var search_orderby_Key = document.getElementById('ddl_search_orderby_Key');
            //alert(search_orderby_Key.options[search_orderby_Key.selectedIndex].value);
            if ('Time' == search_orderby_Key.options[search_orderby_Key.selectedIndex].value) {
                dsTinDangResultArr.sort(function(node1,node2){
                    //var str1 = getValueOfLanCuoiCapNhat(node1);
                    //var str2 = getValueOfLanCuoiCapNhat(node2);
                    var str1 = "";
                    var str2 = "";
                    //alert(node1.childNodes.length);
                    for (var i=0;i<node1.childNodes.length;i++){
                        //alert(node1.childNodes.item(i).nodeName);
                        if (node1.childNodes.item(i).nodeName == 'lancuoicapnhat'){
                            str1 = node1.childNodes.item(i).childNodes.item(0).nodeValue;
                            str2 = node2.childNodes.item(i).childNodes.item(0).nodeValue;
                        }
                    }
                
                    //alert(str1 + str2);
                    if (str1 < str2) {
                        return 1;
                    } else if (str1 > str2) {
                        return -1;
                    } else {
                        return 0;
                    }
                });
            } else if ('Turn' == search_orderby_Key.options[search_orderby_Key.selectedIndex].value){
                dsTinDangResultArr.sort(function(node1,node2){
                    //var str1 = getValueOfLanCuoiCapNhat(node1);
                    //var str2 = getValueOfLanCuoiCapNhat(node2);
                    var str1 = "";
                    var str2 = "";
                    //alert(node1.childNodes.length);
                    for (var i=0;i<node1.childNodes.length;i++){
                        //alert(node1.childNodes.item(i).nodeName);
                        if (node1.childNodes.item(i).nodeName == 'soluotxem'){
                            str1 = node1.childNodes.item(i).childNodes.item(0).nodeValue;
                            str2 = node2.childNodes.item(i).childNodes.item(0).nodeValue;
                        }
                    }

                    //alert(str1 + str2);
                    if (Number(str1) < Number(str2)) {
                        return 1;
                    } else if (Number(str1) > Number(str2)) {
                        return -1;
                    } else {
                        return 0;
                    }
                });
            }

            var startM = page * 5 + 1;
            var endM = (page * 5) + 5;
            document.getElementById('truocSearchButton').disabled = false;
            document.getElementById('sauSearchButton').disabled = false;
            //alert(dsTinDangResultArr.length+"-"+startM+"-"+ endM+"-"+ page);
            if (endM >= dsTinDangResultArr.length) {
                //alert("thay doj end" + dsTinDangResultArr.length);
                endM = dsTinDangResultArr.length;
                document.getElementById('sauSearchButton').disabled = true;
            }
            if (startM <= 1) {
                startM = 1;
                document.getElementById('truocSearchButton').disabled = true;
            }

            var loaitinKeyDDL = document.getElementById('ddlLoaiTin');
            var loaitinTemp = loaitinKeyDDL.options[loaitinKeyDDL.selectedIndex].text;
            if ('Cần Bán' == loaitinTemp){
                tabview_switch('TabView',1);
            } else if ('Cần Mua' == loaitinTemp){
                tabview_switch('TabView',2);
            } else if ('Cho Thuê' == loaitinTemp){
                tabview_switch('TabView',3);
            } else if ('Cần Thuê' == loaitinTemp){
                tabview_switch('TabView',4);
            }
            
            document.getElementById('bSearching').value = 'true';
            //alert(dsTinDangResultArr.length+"-"+startM+"-"+ endM+"-"+ page);
            document.getElementById('nevigatorOfSearching').style.display = 'block';
            document.getElementById('numOfPageSearch').value = page;
            document.getElementById('span_ketquaFrom').innerHTML = startM;
            document.getElementById('span_ketquaTo').innerHTML = endM;
            document.getElementById('bchangeKeySearch').value = 'false';
            
            showresult(dsTinDangResultArr, startM, endM, page);
        }else {
            //tindangDoc = refreshDsTinDang('xml/Tindang.xml', tindangDoc);
            //alert(xmlToString(tindangDoc));
            alert("Không tìm thấy tin đăng phù hợp!");
        }
    //alert(getValueOfLanCuoiCapNhat(dsTinDangResultArr[1]));
    //alert(getValueOfLanCuoiCapNhat(dsTinDangResultArr[2]));
    //showresult(dsTinDangResult);
    }
}

function nodeListTinDangToArray(nodeList){
    var arr = [];
    for (var i=0;i<nodeList.length;i++){
        arr[i] = nodeList.item(i);
    }
    return arr;
}

var t;
//function refreshDsTinDang(url, varToGetNewInfo){
function refreshDsTinDang(){
    var xmlhttp = createXMLHttp();
    
//    xmlhttp.onreadystatechange=function()
//    {
//        //alert(xmlhttp.status);
//        if (xmlhttp.readyState==4 && xmlhttp.status==200)
//        {
//            //tindangDoc=xmlhttp.responseXML;
//            var tindangDoc2 =xmlhttp.responseXML;
//            alert(xmlToString(tindangDoc2));
//            //alert(xmlToString(varToGetNewInfo));
//            //return varToGetNewInfo;
//        }
//    }
    //xmlhttp.open("GET",url,true);
    xmlhttp.open("GET", "xml/Tindang.xml", false);
    xmlhttp.send();
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            tindangDoc=xmlhttp.responseXML;
//            var tindangDoc2 =xmlhttp.responseXML;
//            alert(xmlToString(tindangDoc2));
            //alert(xmlToString(varToGetNewInfo));
            //return varToGetNewInfo;
        }
    //tindangDoc = varToGetNewInfo;
    //alert(xmlToString(tindangDoc));
    t=setTimeout("refreshDsTinDang()",10000);
}

function createXMLHttp(){
    if (typeof XMLHttpRequest != "undefined"){
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        var aVersions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0",
                        "MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.2.0",
                        "MSXML2.XMLHttp","Microsoft.XMLHttp"];
        for (var i=0; i <aVersions.length;i++){
            try{
                var oXmlHttp = new ActiveXObject(aVersions[i]);
            } catch (oError) {
                //alert("asdasd");
            }
        }

    }
    throw new Error("XMLHttp object could be not created");
}
